package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    @Override
    public Integer getCancelledReservationCount(Long userId) {
        //根据用户id查询总的数据条数   id对应的字段是 create_by
        LocalDateTime localDateBeginTime = LocalDateTime.now().withSecond(0).withMinute(0).withHour(0);
        LocalDateTime localDateEndTime = LocalDateTime.now().withSecond(59).withMinute(59).withHour(23);
        Integer count= customerReservationMapper.selectByCreateId(userId,localDateBeginTime,localDateEndTime);

        return  count;
    }

    @Override
    public List<TimeCountVo> queryCountByTime(Long time) {
        //将时间戳转换为年月日
        LocalDateTime begin = Instant.ofEpochMilli(time).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        LocalDateTime end = begin.withDayOfMonth(begin.getDayOfMonth() + 1);

        //查询今天每个时间预约了多少个用户
       List<TimeCountVo> lists = customerReservationMapper.queryCountByTime(begin,end);
       //如果查到的有数据就进行逻辑操作 然后返回
       log.info("查询到已经有预约记录的时间{}",lists);
       if (!lists.isEmpty()){
        //算出每个时间剩余的时间
        List<TimeCountVo> vos = lists.stream().map(list -> {
            list.setCount(6 - list.getCount());
            return list;
        }).collect(Collectors.toList());
        log.info("要返回前端的当前时间剩余预约的次数:{}",vos);
           return  vos;
       }
       //没有查到数据就直接返回 空集合
      return lists;
    }

    @Override
    public  void addReservation(ReservationDto dto) {
        //dto转entity
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        //查询当前时间是否预约已满
        //long time = dto.getTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        synchronized (UserThreadLocal.getUserId()) {
            Integer count = customerReservationMapper.selectByTime(dto.getTime());
            if (count<6){
                reservation.setCreateBy(UserThreadLocal.getUserId());
                reservation.setStatus(0);
                customerReservationMapper.insert(reservation);
            }else {
                throw new BaseException(BasicEnum.CUSTOMER_RESERVATION_ADD_FAIL);
            }
        }

    }

    @Override
    public PageResponse<ReservationVo> queryPage(Integer status, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        Page<ReservationVo> voPage= customerReservationMapper.selectPage(status,UserThreadLocal.getUserId());
       return PageResponse.of(voPage, ReservationVo.class);
    }

    @Override
    public void modifyStatus(Long id) {
            Reservation reservation=new Reservation();
            reservation.setId(id);
            reservation.setUpdateBy(UserThreadLocal.getUserId());
            reservation.setStatus(2);
        customerReservationMapper.update(reservation);

    }
}
